/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package thesis.haipv.qa.persistence;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author PHANHAI
 */
public class ExperienceTrustDAO {

    private DatabaseContext db;

    public ExperienceTrustDAO() {
        db = DatabaseContext.getInstance();
    }

    public void createExperienceTrust(String userId, String partnerId, float exptrust, long time) {
        String query = "INSERT INTO experiencetrust "
                + "(ExpTrust, UserId, PartnerId, CreateTime) "
                + "VALUES(?, ?, ?, ?)";
        try {
            PreparedStatement ps = db.getConnection().prepareStatement(query);
            ps.setFloat(1, exptrust);
            ps.setString(2, userId);
            ps.setString(3, partnerId);
            ps.setLong(4, time);
            ps.executeUpdate();
            ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void updateExperienceTrust(String userId, String partnerId, float trust, long createtime) {
        String query = "UPDATE experiencetrust "
                + "SET ExpTrust = ?, CreateTime = ? "
                + "WHERE UserId = ? AND PartnerId = ?";
        try{
            PreparedStatement ps = db.getConnection().prepareStatement(query);
            ps.setFloat(1, trust);
            ps.setLong(2, createtime);
            ps.setString(3, userId);
            ps.setString(4, partnerId);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    public float getExperienceTrust(String userId, String partnerId) {
        float trust = -1;
        String query = "SELECT ExpTrust FROM experiencetrust "
                + "WHERE UserId = ? AND PartnerId = ? LIMIT 1";
        try {
            PreparedStatement ps = db.getConnection().prepareStatement(query);
            ps.setString(1, userId);
            ps.setString(2, partnerId);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                trust = rs.getFloat("ExpTrust");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return trust;
    }

    public ArrayList<Float> getReferienceTrust(String userId) {
        ArrayList<Float> trusts = new ArrayList<>();
        String query = "SELECT ExpTrust FROM experiencetrust "
                + "WHERE PartnerId = ?";
        try {
            PreparedStatement ps = db.getConnection().prepareStatement(query);
            ps.setString(1, userId);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                trusts.add(rs.getFloat("ExpTrust"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return trusts;
    }
}
